package com.example.yydm666.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.yydm666.entity.Product;
import com.example.yydm666.entity.ProductCategory;
import com.example.yydm666.entity.User;
import com.example.yydm666.service.CartService;
import com.example.yydm666.service.ProductCategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-03-28
 */
@Controller
@RequestMapping("/productCategory")
@Api(tags = "产品类别模块")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private CartService cartService;

    @GetMapping("/main")
    public ModelAndView list(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        //封装商品分类菜单
        modelAndView.addObject("list", this.productCategoryService.buildProductCategoryMenu());
        //封装一级分类对应的商品信息
        modelAndView.addObject("levelOneProductList", this.productCategoryService.findAllProductByCategoryLevelOne());
//        User user = (User) session.getAttribute("user");
//        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        //判断是否为登录用户
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        } else {
            //登录用户
            //查询该用户的购物车记录
            modelAndView.addObject("cartList", this.cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }

//    @RequestMapping("/productCategory1/{type}")
//    @ResponseBody
//    public List<ProductCategory> productCategory(@PathVariable("type") Integer type){
//        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper();
//        wrapper.eq(ProductCategory::getType,type);
//        List<ProductCategory> list = productCategoryService.list(wrapper);
//        return list;
//    }

    @GetMapping("/selectId")
    @ResponseBody
    public List<ProductCategory> selectId() {
        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();

        List<ProductCategory> list = productCategoryService.list(wrapper);
        return list;
    }


}

